package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.ThirdModeIDType;
import com.zjdiepu.www.dao.partner.ThBusinessRecordMapper;
import com.zjdiepu.www.ibiz.partner.IThBusinessRecordService;
import com.zjdiepu.www.model.partner.dto.ThBusinessRecord;
import com.zjdiepu.www.model.partner.ext.ThBusinessRecordExt;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 第三方信息履历，保存各阶段信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月25日 13时41分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThBusinessRecordServiceImpl extends BaseService implements IThBusinessRecordService {
	public static final Logger logger = LoggerFactory.getLogger(ThBusinessRecordServiceImpl.class);

	@Autowired
	private ThBusinessRecordMapper thBusinessRecordMapper;
	
	@Override
	public ThBusinessRecord saveOrUpdateThBusinessRecord(String busiId,
			String dataId, String busiType,String xeApplyNo,
			String describeEvent,ThirdModeIDType thirdModeIDType,
			String currentUserId, String currentUserName) {
		ThBusinessRecord thBusinessRecordParam = new ThBusinessRecord();
		thBusinessRecordParam.setDpStatus(DpStatus.NORMAL.getCode());
		thBusinessRecordParam.setBusiId(busiId);
		if(null == thirdModeIDType) {
			// 不可能为null，为null时必定参数传错，调试阶段必须发现
			ExceptionHelper.thow("9999", "保存错误处理时参数模块ID为空");
		}
		thBusinessRecordParam.setThirdModeId(thirdModeIDType.getCode());
		ThBusinessRecord thBusinessRecord = thBusinessRecordMapper.selectRefined(thBusinessRecordParam);
		boolean insert = false;
		if(null == thBusinessRecord) {
			insert = true;
			thBusinessRecord = new ThBusinessRecord();
			thBusinessRecord.initInsertData(currentUserId, currentUserName);
		} else {
			thBusinessRecord.initUpdateData(currentUserId, currentUserName);
		}
		thBusinessRecord.setDataId(dataId);
		thBusinessRecord.setBusiType(busiType);
		thBusinessRecord.setBusiId(busiId);
		thBusinessRecord.setThirdId(xeApplyNo);
		thBusinessRecord.setThirdModeId(thirdModeIDType.getCode());
		thBusinessRecord.setThirdModeName(thirdModeIDType.getValue());
		//履历描述
		thBusinessRecord.setRecordEvent(describeEvent);

		if(insert) {
			thBusinessRecordMapper.insert(thBusinessRecord);
		} else {
			thBusinessRecordMapper.updateByPrimaryKeySelective(thBusinessRecord);
		}
		return thBusinessRecord;
	}	
	
	@Override
	public void saveUpdateAndDelete(ThBusinessRecord thBusinessRecord, OperateType operType) {
		switch (operType) {
		case add:
			thBusinessRecord.initInsertData(thBusinessRecord.getModifier(), thBusinessRecord.getModifierName());
			thBusinessRecordMapper.insert(thBusinessRecord);
			break;
		case edit:
			thBusinessRecord.initUpdateData(thBusinessRecord.getModifier(), thBusinessRecord.getModifierName());
			// 影响的记录数
			int updateCount = thBusinessRecordMapper.updateByPrimaryKeySelective(thBusinessRecord);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thBusinessRecord.initDeleteData(thBusinessRecord.getModifier(), thBusinessRecord.getModifierName());
			// 影响的记录数
			int deleteCount = thBusinessRecordMapper.updateByPrimaryKeySelective(thBusinessRecord);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThBusinessRecord> selectByExt(ThBusinessRecordExt thBusinessRecordExt) {
		return thBusinessRecordMapper.selectByExt(thBusinessRecordExt);
	}
	
	@Override
	public int countByExt(ThBusinessRecordExt thBusinessRecordExt){
		return thBusinessRecordMapper.countByExt(thBusinessRecordExt);
	}
	
	@Override
	public ThBusinessRecord selectById(String id) {
		return thBusinessRecordMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thBusinessRecordMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThBusinessRecord thBusinessRecord) {
		return thBusinessRecordMapper.countRefined(thBusinessRecord);
	}

	@Override
	public ThBusinessRecord selectRefined(ThBusinessRecord thBusinessRecord) {
		return thBusinessRecordMapper.selectRefined(thBusinessRecord);
	}
	
	@Override
	public int deletedRefined(ThBusinessRecord thBusinessRecord) {
		return thBusinessRecordMapper.deletedRefined(thBusinessRecord);
	}

	@Override
	public boolean repeat(ThBusinessRecord thBusinessRecord) {
		int count = countRefined(thBusinessRecord);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thBusinessRecord.getId())) {
			return true;
		}
		ThBusinessRecord dbItem = selectRefined(thBusinessRecord);
		if(null == dbItem) {
			return false;
		}
		return !thBusinessRecord.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thBusinessRecordMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThBusinessRecord> thBusinessRecords) {
		return thBusinessRecordMapper.insertBatch(thBusinessRecords);
	}
}
